<template>
	<view class="workbench-wrapper">
		<view class="top-box">
			<u-avatar :src="userInfo.headimgurl"></u-avatar>
			<text>上午好，{{ userInfo.nickname || '--' }}</text>
		</view>
		
		<view class="notice-box">
			<view class="title">最新公告</view>
			
			<view class="notice-list">
				<view v-for="item in noticeInfo" :key="item.id">{{ item.content }}</view>
			</view>
		</view>
		
		<view class="rank-box">
			<view class="title">消费排行</view>
			
			<template v-if="rankInfo.length">
				<view class="chart-box">
					<qiun-data-charts 
						type="pie"
						:opts="opts"
						:chartData="chartData"
					/>
				</view>
				
				<view class="rank-list">
					<view class="title-box">
						<view>客户</view>
						<view>消费</view>
						<view>余额</view>
					</view>
					<template v-if="rankInfo.length">
						<view class="content-box" v-for="item in rankInfo" :key="item.id">
							<view>{{ item.nickname }}</view>
							<view>{{ item.orderPrice }}</view>
							<view>{{ item.money }}</view>
						</view>
					</template>
				</view>
			</template>
			<view class="no-data-box" v-if="showNoData">暂无数据</view>
		</view>
	</view>
</template>

<script>
	import { getMemberDetail, appHomePage } from '@/api/index.js'
	
	export default {
		data() {
			return {
				userInfo: {
					headimgurl: '',
					nickname: ''
				},
				noticeInfo: [],
				rankInfo: [],
				showNoData: false,
				chartData: {},
				opts: {
					legend: {
						show: false
					},
					dataLabel: false,
					color: ["#1890FF", "#91CB74"],
					padding: [5, 5, 5, 5],
					enableScroll: false,
					extra: {
						pie: {
							activeOpacity: 0.5,
							activeRadius: 10,
							offsetAngle: 0,
							labelWidth: 15,
							border: true,
							borderWidth: 1,
							borderColor: "#FFFFFF"
						}
					}
				}
			}
		},
		onShow() {
		    this.getUserInfo();
		    this.getHomeData();
		},
		methods: {
			getUserInfo() {
				const userInfo = uni.getStorageSync('userInfo')
				
				getMemberDetail({
					memberId: userInfo.id
				}).then(res => {
					uni.setStorageSync('userInfo', res.data)
					this.userInfo = res.data
				})
			},
			getHomeData() {
				appHomePage().then(res => {
					const { noticeList, rank } = res.data
					this.noticeInfo = noticeList
					this.rankInfo = rank || []
					this.showNoData = this.rankInfo.length ? false : true
					
					this.getServerData();
				})
			},
			getServerData() {
				const data = this.rankInfo.map(item => {
					return {
						name: item.nickname,
						value: item.orderPrice
					}
				})
			    let res = {
			        series: [
			          {
			            data: data
			          }
			        ]
			      };
			    this.chartData = JSON.parse(JSON.stringify(res));
			},
		}
	}
</script>

<style lang="scss">
.workbench-wrapper {
	display: flex;
	flex-direction: column;
	height: 100%;
	overflow: auto;
	
	.top-box {
		display: flex;
		align-items: center;
		padding: 20rpx 30rpx;
		color: #fff;
		background-color: #195ED7;
		
		text {
			margin-left: 20rpx;
			font-weight: bold;
		}
	}
	
	.notice-box {
		padding: 20rpx 30rpx 0;
		background-color: #fff;
		
		.title {
			font-size: 32rpx;
			font-weight: bold;
		}
		
		.notice-list {
			view {
				padding: 16rpx 0;
				border-bottom: 1px solid #ebebeb;
				
				&:last-of-type {
					border-bottom: 0;
				}
			}
		}
	}
	
	.rank-box {
		margin-top: 20rpx;
		padding: 20rpx 30rpx;
		background-color: #fff;
		
		.title {
			font-size: 32rpx;
			font-weight: bold;
		}
		
		.chart-box {
			width: 100%;
			height: 320rpx;
			margin-top: 20rpx;
		}
		
		.rank-list {
			.title-box {
				display: flex;
				align-items: center;
				padding: 20rpx 0;
				
				view {
					flex-shrink: 0;
					width: 33.3%;
					text-align: center;
					font-weight: bold;
				}
			}
			
			.content-box {
				display: flex;
				align-items: center;
				
				view {
					flex-shrink: 0;
					width: 33.3%;
					padding: 10rpx 0;
					text-align: center;
				}
			}
		}
	}
	
	.no-data-box {
		padding: 40rpx 0;
		text-align: center;
		color: #999;
	}
}
</style>
